package com.example.a14110100420.yuejian.Activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.a14110100420.yuejian.Fragment.ClubFragment;
import com.example.a14110100420.yuejian.Fragment.CoachFragment;
import com.example.a14110100420.yuejian.Fragment.MeFragment;
import com.example.a14110100420.yuejian.Fragment.ShaituFragment;
import com.example.a14110100420.yuejian.Fragment.TrainFragment;
import com.example.a14110100420.yuejian.R;

public class MainActivity extends FragmentActivity implements View.OnClickListener {
    private LinearLayout TabClub;
    private LinearLayout TabCoach;
    private LinearLayout TabMe;
    private LinearLayout TabShaitu;
    private LinearLayout TabTrain;

    private ImageButton ImgClub;
    private ImageButton ImgCoach;
    private ImageButton ImgMe;
    private ImageButton ImgShaitu;
    private ImageButton ImgTrain;
    private TextView Ttrain;
    private TextView Tclub;
    private TextView Tshaitu;
    private TextView Tme;

    private Fragment Tab01;
    private Fragment Tab02;
    private Fragment Tab03;
    private Fragment Tab04;
    private Fragment Tab05;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
        setSelect(0);



    }
    private void initEvent(){
        TabClub.setOnClickListener(this);
        TabTrain.setOnClickListener(this);
        TabCoach.setOnClickListener(this);
        TabMe.setOnClickListener(this);
        TabShaitu.setOnClickListener(this);

    }
    private void initView(){
        TabShaitu =findViewById(R.id.id_tab_shaitu);
        TabTrain = findViewById(R.id.id_tab_train);
        TabClub = findViewById(R.id.id_tab_club);
        TabCoach = findViewById(R.id.id_tab_coach);
        TabMe = findViewById(R.id.id_tab_personal);

        ImgShaitu = findViewById(R.id.id_tab_shaitu_img);
        ImgTrain = findViewById(R.id.id_tab_train_img);
        ImgClub = findViewById(R.id.id_tab_club_img);
        ImgCoach = findViewById(R.id.id_tab_coach_img);
        ImgMe = findViewById(R.id.id_tab_personal_img);

        Ttrain = findViewById(R.id.id_TTrain);
        Tclub = findViewById(R.id.id_TClub);
        Tshaitu = findViewById(R.id.id_TShaitu);
        Tme = findViewById(R.id.id_TMe);

    }
    
    private void setSelect(int i){
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i)
        {
            case 0:
                if (Tab01==null)
                {
                    Tab01 = new CoachFragment();
                    transaction.add(R.id.id_content, Tab01);

                }
                else{
                    transaction.show(Tab01);
                }
                ImgCoach.setImageResource(R.drawable.ic_coach);

                break;
            case 1:
                if (Tab02==null)
                {
                    Tab02 = new TrainFragment();
                    transaction.add(R.id.id_content,Tab02);
                }else {
                    transaction.show(Tab02);
                }
                ImgTrain.setImageResource(R.drawable.ic_train);
                Ttrain.setTextColor(getResources().getColor(R.color.white));
                break;
            case 2:
                if (Tab03==null)
                {
                    Tab03 = new ClubFragment();
                    transaction.add(R.id.id_content,Tab03);
                }else {
                    transaction.show(Tab03);
                }
                ImgClub.setImageResource(R.drawable.ic_club);
                Tclub.setTextColor(getResources().getColor(R.color.white));
                break;
            case 3:
                if (Tab04==null)
                {
                    Tab04 = new ShaituFragment();
                    transaction.add(R.id.id_content,Tab04);
                }else
                {
                    transaction.show(Tab04);
                }
                ImgShaitu.setImageResource(R.drawable.ic_shaitu);
                Tshaitu.setTextColor(getResources().getColor(R.color.white));
                break;
            case 4:
                if (Tab05==null){
                    Tab05 = new MeFragment();
                    transaction.add(R.id.id_content,Tab05);
                }else{
                    transaction.show(Tab05);
                }
                ImgMe.setImageResource(R.drawable.ic_personal);
                Tme.setTextColor(getResources().getColor(R.color.white));
                break;

            default:
                break;
        }
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (Tab01 != null)
        {
            transaction.hide(Tab01);
        }
        if (Tab02 != null)
        {
            transaction.hide(Tab02);
        }
        if (Tab03 != null)
        {
            transaction.hide(Tab03);
        }
        if (Tab04 != null)
        {
            transaction.hide(Tab04);
        }
        if (Tab05!=null){
            transaction.hide(Tab05);
        }
    }

    @Override
    public void onClick(View view) {
        resetImgs();
        switch (view.getId())
        {
            case R.id.id_tab_coach:
                setSelect(0);
                break;
            case R.id.id_tab_train:
                setSelect(1);
                break;
            case R.id.id_tab_club:
                setSelect(2);
                break;
            case R.id.id_tab_shaitu:
                setSelect(3);
                break;
            case R.id.id_tab_personal:
                setSelect(4);
                break;
            default:
                break;
        }

    }

    private void resetImgs(){
        ImgCoach.setImageResource(R.drawable.ic_coach_nor);
        ImgTrain.setImageResource(R.drawable.ic_train_nor);
        ImgClub.setImageResource(R.drawable.ic_club_nor);
        ImgShaitu.setImageResource(R.drawable.ic_shaitu_nor);
        ImgMe.setImageResource(R.drawable.ic_personal_nor);
        Tme.setTextColor(getResources().getColor(R.color.grey));
        Tshaitu.setTextColor(getResources().getColor(R.color.grey));
        Tclub.setTextColor(getResources().getColor(R.color.grey));
        Ttrain.setTextColor(getResources().getColor(R.color.grey));

    }
}
